//
//  Inter_thread_communication_VC.m
//  DXC
//
//  Created by TOPTEAM on 2017/5/8.
//  Copyright © 2017年 TOPTEAM. All rights reserved.
//

#import "Inter_thread_communication_VC.h"

@interface Inter_thread_communication_VC ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (nonatomic,assign)BOOL isWay1;
@end

@implementation Inter_thread_communication_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    _isWay1=YES;
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"线程间通信";
    
    
    // Do any additional setup after loading the view.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (_isWay1) {
        [self Way_1];
        _isWay1=NO;
    }else{
        [self Way_2];
        _isWay1=YES;
    }
    
    
}


-(void)Way_1{
    
    [NSThread detachNewThreadSelector:@selector(download) toTarget:self withObject:nil];
}

-(void)Way_2{
    //1.开子线程下载图片
    //创建队列(并发)
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    //异步函数
    dispatch_async(queue, ^{
        
        //1.获取url地址
        NSURL *url = [NSURL URLWithString:@"http://www.huabian.com/uploadfile/2015/0914/20150914014032274.jpg"];
        
        //2.下载图片
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        //3.把二进制数据转换成图片
        UIImage *image = [UIImage imageWithData:data];
        
        NSLog(@"----%@",[NSThread currentThread]);
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.imgView.image = image;
            NSLog(@"+++%@",[NSThread currentThread]);
        });
    });
}

-(void)download2
{
    //下载图片
    //1.获取url地址
    NSURL *url = [NSURL URLWithString:@"http://pic1.ooopic.com/uploadfilepic/sheji/2010-01-13/OOOPIC_1982zpwang407_20100113f68118f451f282f4.jpg"];
    //2.下载图片
    NSDate *start = [NSDate date];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSDate *end = [NSDate date];
    
    NSLog(@"+++++%f",[end timeIntervalSinceDate:start]);
    //3.把二进制数据转换成图片
    UIImage *image = [UIImage imageWithData:data];
    
    NSLog(@"----%@",[NSThread currentThread]);
    //4.回到主线程刷新UI
    //    waitUntilDone:
    //    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
    //    [self.imgView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    
    [self.imgView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
    
    
}
-(void)download
{
    //下载图片
    //1.获取url地址
    NSURL *url = [NSURL URLWithString:@"http://pic1.ooopic.com/uploadfilepic/sheji/2010-01-13/OOOPIC_1982zpwang407_20100113f68118f451f282f4.jpg"];
    //2.下载图片
    CFTimeInterval start = CFAbsoluteTimeGetCurrent();
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    CFTimeInterval end = CFAbsoluteTimeGetCurrent();
    
    NSLog(@"+++++%f",end - start);
    //3.把二进制数据转换成图片
    UIImage *image = [UIImage imageWithData:data];
    
    NSLog(@"----%@",[NSThread currentThread]);
    //4.回到主线程刷新UI
    //    waitUntilDone:
    //    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
    //    [self.imgView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    
    [self.imgView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
    
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
